package com.example.myapplication.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.example.myapplication.R;

import java.util.zip.Inflater;


public class ViewPagerAdapter extends PagerAdapter {
    private Context mContext;
    private int[] mImages;
    public ViewPagerAdapter(Context context, int[] images) {
        mContext = context;
        mImages = images;
    }

    @Override
    public int getCount() {
        return mImages.length;    // 代表页面的数量
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    // 添加每一页的显示内容
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
//        TextView textView = new TextView(mContext);
//        textView.setText("this is item " + position);
//        container.addView(textView);
//        Log.i("tag", position+"");
        View view =
                LayoutInflater.from(mContext).inflate(R.layout.banner_item_view, container, false);
        ImageView imageView = view.findViewById(R.id.image);
        imageView.setImageResource(mImages[position]);
        container.addView(view);

        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
        Log.i("tag", "remove " + position);
    }
}
